﻿using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Net;

namespace BlobsMigration
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Migrate();
        }

        private static void Migrate()
        {
            //SOURCE
            var sourceCloudStorageCredentials = new StorageCredentials("[sourceStorageAccountName]", "[yourSourceStorageAccountAccessKey]");
            var sourceCloudStorageAccount = new CloudStorageAccount(sourceCloudStorageCredentials, true);
            var sourceCloudBlobClient = sourceCloudStorageAccount.CreateCloudBlobClient();

            //DESTINY
            var destinyCloudStorageCredentials = new StorageCredentials("[destinyStorageAccountName]", "[yourDestinyStorageAccountAccessKey]");
            var destinyCloudStorageAccount = new CloudStorageAccount(destinyCloudStorageCredentials, true);
            var destinyCloudBlobClient = destinyCloudStorageAccount.CreateCloudBlobClient();

            //COPY PUBLIC BLOBS
            //var sourceContainers = sourceCloudBlobClient.ListContainers();
            //foreach (var sourceContainer in sourceContainers)
            //{
            //    Console.WriteLine(sourceContainer.Name);
            //    var destinyContainer = destinyCloudBlobClient.GetContainerReference(sourceContainer.Name);
            //    if (destinyContainer.CreateIfNotExists())
            //    {
            //        destinyContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            //        var sourceContainerBlobs = sourceContainer.ListBlobs();
            //        foreach (var sourceContainerBlob in sourceContainerBlobs)
            //        {
            //            var sourceBlob = (ICloudBlob)sourceContainerBlob;
            //            Console.WriteLine(sourceBlob.Name);

            //            if (sourceBlob.Properties.BlobType == BlobType.BlockBlob)
            //            {
            //                CloudBlockBlob destinyBlob = destinyContainer.GetBlockBlobReference(sourceBlob.Name);
            //                destinyBlob.StartCopyFromBlob((CloudBlockBlob)sourceBlob);
            //            }
            //            else
            //            {
            //                CloudPageBlob destinyBlob = destinyContainer.GetPageBlobReference(sourceBlob.Name);
            //                destinyBlob.StartCopyFromBlob((CloudPageBlob)sourceBlob);
            //            }
            //        }
            //    }

            //    Console.WriteLine();
            //}

            //COPY PRIVATE BLOBS
            var sourceContainers = sourceCloudBlobClient.ListContainers();
            foreach (var sourceContainer in sourceContainers)
            {
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
                containerPermissions.SharedAccessPolicies.Add("readpolicy", new SharedAccessBlobPolicy 
                {
                    SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-3),
                    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(7),
                    Permissions = SharedAccessBlobPermissions.Read
                });
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;
                sourceContainer.SetPermissions(containerPermissions);

                string sasToken = sourceContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy(), "readpolicy");

                Console.WriteLine(sourceContainer.Name);
                var destinyContainer = destinyCloudBlobClient.GetContainerReference(sourceContainer.Name);
                if (destinyContainer.CreateIfNotExists())
                {
                    destinyContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

                    var sourceContainerBlobs = sourceContainer.ListBlobs();
                    foreach (var sourceContainerBlob in sourceContainerBlobs)
                    {
                        var sourceBlob = (ICloudBlob)sourceContainerBlob;
                        Console.WriteLine(sourceBlob.Name);

                        if (sourceBlob.Properties.BlobType == BlobType.BlockBlob)
                        {
                            CloudBlockBlob destinyBlob = destinyContainer.GetBlockBlobReference(sourceBlob.Name);
                            destinyBlob.StartCopyFromBlob(new Uri(sourceBlob.StorageUri.PrimaryUri.AbsoluteUri + sasToken));
                        }
                        else
                        {
                            CloudPageBlob destinyBlob = destinyContainer.GetPageBlobReference(sourceBlob.Name);
                            destinyBlob.StartCopyFromBlob(new Uri(sourceBlob.StorageUri.PrimaryUri.AbsoluteUri + sasToken));
                        }
                    }
                }

                Console.WriteLine();
            }

            Console.WriteLine("MIGRATION FINISHED");
            Console.ReadLine();
        }
    }
}